package com.kangkang.leetcode10;

public class Test45 {
    public static void main(String[] args) {
        int[] a= {1,2,3};
        System.out.println(Solution45.jump1(a));
    }
}
class Solution45 {
    public static int jump(int[] nums) {
        //反向法
        int now = nums.length-1;
        int step = 0;
        while (now > 0){
            for (int i = 0; i <= now; i++) {
                if(nums[i] >= now - i){
                    now = i;
                    step++;
                    break ;
                }
            }
        }
        return step;
    }
    public static int jump1(int[] nums) {
        //正向法

        int step = 0;
        int end = 0;
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            max = Math.max(max,i + nums[i]);
            if(i == end){//表示找到了下一个跳转的位置为 使 max 最大的那个点(next)
                end = max; //找下一个点在next到max(end)之间
                step++;
            }
        }
        return step;
    }
}